namespace NL.Advanced
{

    public enum BuffReplaceType
    {
        None = 0,  //不处理..一直堆叠..
        Replace = 1,//达到最大层数后，新的BUFF替换计时最少的一个BUFF
        //DelayTime = 2,//达到最大层数后，新的BUFF重置倒计时最少的一个BUFF上
        Refresh = 3,//刷新，所有层数时间同步且刷新


        ChangeIdWhenMax = 4,//达到最大层数后，再添加就会升级buff
    }
    public class Buff : _BuffBase
    {

        private static IFBuffAction<int> _ONInfoQuery;
        public static void WAITInfoQuery(IFBuffAction<int> on)
        {
            _ONInfoQuery = on;
        }

        protected override void Start()
        {
            if (ID > 0 && _ONInfoQuery != null)
            {
                var info = _ONInfoQuery.Invoke(ID);
                gameObject.INFOSet(info);

                _LoadInfo(info);
            }
            base.Start();
        }

        protected virtual void _LoadInfo(IFBuff info)
        {

            if (info == null) return;

            Group = info["group"].AsIntDefault(Group);
            OverlayMax = info["overlay"].AsIntDefault(OverlayMax);
            Replace = info["replace"].AsEnum<BuffReplaceType>(Replace);



            Life = info["life"].AsFloatDefault(Life);
            Duration = info["duration"].AsFloatDefault(Duration);

            Delay = info["delay"].AsFloatDefault(Delay);
            Property = info["property"].AsString;
            PropertyBeat = info["property_beat"].AsString;
            URL_EFF_Start = info["eff_start"].AsString;
            URL_EFF_Beat = info["eff_beat"].AsString;

            // 读取状态集合//
            StateMask = ObjState.None;
            if (!string.IsNullOrEmpty(info["state"].AsString))
            {
                var all = info["state"].AsString.Split(NLConst.CHAR_ARRAY);
                if (all != null)
                {
                    foreach (var a in all)
                    {
                        var state = a.AsEnum<ObjState>(ObjState.None);
                        if (state != ObjState.None)
                            StateMask |= state;
                    }
                }
            }

            //拼接attacker
            for (int i = 1; i <= 16; i++)
            {
                string atk = info[$"attacker{i}"].AsString;
                if (!string.IsNullOrEmpty(atk))
                {
                    gameObject.AddComponent<Attacker>().Set(atk);
                }
            }


            LOG.Add($"[Buff.LoadFromInfo({ID})] :");
        }







    }

    public static class BuffExx
    {
        public static Buff ADDBuff(this IBuffOwner who, string property, float life)
        {
            if (who == null) return null;

            var buff = who.gameObject.ADDChild("~buff").AddComponent<Buff>();
            buff.SetLife(life).SetProperty(property);

            return buff;
        }
        public static Buff ADDBuff(this IBuffOwner who, ObjState state, float life)
        {
            if (who == null) return null;

            var buff = who.gameObject.ADDChild("~buff").AddComponent<Buff>();
            buff.SetLife(life).SetObjState(state);

            return buff;
        }
        public static Buff ADDBuff(this IBuffOwner who, int buffid, float life)
        {
            if (who == null) return null;

            var buff = who.gameObject.ADDChild("~buff").AddComponent<Buff>();
            buff.SetID(buffid).SetLife(life);

            return buff;
        }
    }
}